package com.romain.risotto.hud;

import org.andengine.entity.Entity;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

public class ProgressBar extends Entity {
	// ===========================================================
	// Constants          
	// ===========================================================
	private static final float FRAME_LINE_WIDTH = 2f;
	// ===========================================================          
	// Fields         
	// =========================================================== 
	private final Line[] mFrameLines = new Line[4];
	private final Rectangle mBackgroundRectangle;
	private final Rectangle mProgressRectangle;
	
	private final float mPixelsPerPercentRatio;
	// ===========================================================          
	// Constructors          
	// =========================================================== 
	public ProgressBar(final float pX, final float pY, final float pWidth, final float pHeight,VertexBufferObjectManager vbom) {
		super();
		
		this.mBackgroundRectangle = new Rectangle(pX, pY, pWidth, pHeight,vbom);
		
		this.mFrameLines[0] = new Line(pX, pY, pX+pWidth, pY, FRAME_LINE_WIDTH,vbom); //Top line.
		this.mFrameLines[1] = new Line(pX + pWidth, pY, pX + pWidth, pY + pHeight, FRAME_LINE_WIDTH,vbom); //Right line.
		this.mFrameLines[2] = new Line(pX + pWidth, pY + pHeight, pX, pY + pHeight, FRAME_LINE_WIDTH,vbom); //Bottom line.
		this.mFrameLines[3] = new Line(pX, pY + pHeight, pX, pY, FRAME_LINE_WIDTH,vbom); //Left line.
		
		this.mProgressRectangle = new Rectangle(pX, pY, pWidth, pHeight,vbom);
		
		super.attachChild(this.mBackgroundRectangle); //This one is drawn first.
		super.attachChild(this.mProgressRectangle); //The progress is drawn afterwards.
		for(int i = 0; i < this.mFrameLines.length; i++)
			super.attachChild(this.mFrameLines[i]); //Lines are drawn last, so they'll override everything.
		
		this.mPixelsPerPercentRatio = pWidth / 100;
	}
	// ===========================================================          
	// Getter & Setter          
	// =========================================================== 
	public void setBackColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
		this.mBackgroundRectangle.setColor(pRed, pGreen, pBlue, pAlpha);
	}
	public void setFrameColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
		for(int i = 0; i < this.mFrameLines.length; i++)
			this.mFrameLines[i].setColor(pRed, pGreen, pBlue, pAlpha);
	}
	public void setProgressColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
		this.mProgressRectangle.setColor(pRed, pGreen, pBlue, pAlpha);
	}
	/**
	 * Set the current progress of this progress bar.
	 * @param pProgress is <b> BETWEEN </b> 0 - 100.
	 */
	public void setProgress(final float pProgress) {
		if(pProgress < 0)
			this.mProgressRectangle.setWidth(0); //This is an internal check for my specific game, you can remove it.
		this.mProgressRectangle.setWidth(this.mPixelsPerPercentRatio * pProgress);
	}
	// ===========================================================          
	// Methods for/from SuperClass/Interfaces          
	// ===========================================================  
	
	// ===========================================================          
	// Methods          
	// ===========================================================  
	
	// ===========================================================          
	// Inner and Anonymous Classes          
	// ===========================================================  
	
}